home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime vr / vrmakeobject / application files / comapplication.c next >
Encoding:
Text File  |  2000-09-28  |  11.6 KB  |  459 lines

  1. //////////
  2. //
  3. //    File:        ComApplication.c
  4. //
  5. //    Contains:    Application-specific code for basic VR movie display and control.
  6. //                This file is used for BOTH MacOS and Windows.
  7. //
  8. //    Written by:    Tim Monroe
  9. //                Based (heavily!) on the MovieShell code written by Apple DTS.
  10. //
  11. //    Copyright:    © 1994-1998 by Apple Computer, Inc., all rights reserved.
  12. //
  13. //    Change History (most recent first):
  14. //
  15. //       <10>         10/23/97    rtm        moved InitializeQTVR to InitApplication, TerminateQTVR to StopApplication
  16. //       <9>         10/13/97    rtm        reworked HandleApplicationMenu to use menu identifiers
  17. //       <8>         09/11/97    rtm        merged MacApplication.c and WinApplication.c into ComApplication.c
  18. //       <7>         08/21/97    rtm        first file for Windows; based on MacApplication.c for Mac sample code
  19. //       <6>         06/04/97    rtm        removed call to QTVRUtils_IsQTVRMovie in InitApplicationWindowObject
  20. //       <5>         02/06/97    rtm        fixed window resizing code
  21. //       <4>         12/05/96    rtm        added hooks into MacFramework.c: StopApplication, InitApplicationWindowObject
  22. //       <3>         12/02/96    rtm        added cursor updating to DoIdle
  23. //       <2>         11/27/96    rtm        conversion to personal coding style; added preliminary QTVR support
  24. //       <1>         12/21/94    khs        first file
  25. //       
  26. //
  27. //    VRShell is a simple QuickTime VR viewer framework. It demonstrates how to incorporate 
  28. //    QuickTime and QuickTime VR movies into an application. VRShell is based heavily on the 
  29. //    MovieShell QuickTime playback framework included with the DTS QT sample code.
  30. //
  31. //////////
  32.  
  33. // header files
  34. #include "ComApplication.h"
  35. #include "VRMakeObject.h"
  36.  
  37. // global variables for Macintosh code
  38. #if TARGET_OS_MAC
  39. #endif
  40.  
  41. // global variables for Windows code
  42. #if TARGET_OS_WIN32
  43. extern HWND                ghWnd;                                    // the MDI frame window; this window has the menu bar
  44. extern int                gNumWindowsOpen;
  45. extern LPSTR            gCmdLine;
  46. #endif
  47.  
  48. long                    gMaxMilliSecToUse = 0L;
  49. Boolean                    gQTVRMgrIsPresent = false;                // is the QuickTime VR Manager available?        
  50. long                    gQTVRMgrVersion = 0L;                    // the version of the QuickTime VR Manager
  51.  
  52. extern UInt32            gVersionToCreate;                        // the version of the file format we create
  53.  
  54.  
  55. //////////
  56. //
  57. // InitApplication
  58. // Do any application-specific initialization.
  59. //
  60. // The theStartPhase parameter determines which "phase" of application start-up is executed,
  61. // *before* the MDI frame window is created or *after*. This distinction is relevant only on
  62. // Windows, so on MacOS, you should always use kInitAppPhase_BothPhases.
  63. //
  64. //////////
  65.  
  66. void InitApplication (UInt32 theStartPhase)
  67. {
  68.     // ***do any start-up activities that should occur before the MDI frame window is created
  69.     if (theStartPhase & kInitAppPhase_BeforeCreateFrameWindow) {
  70.  
  71.         // make sure that the QuickTime VR Manager is available in the present operating environment;
  72.         // if it is, get its version and (if necessary) initialize it
  73.         if (QTVRUtils_IsQTVRMgrInstalled()) {
  74.             gQTVRMgrIsPresent = true;
  75.             gQTVRMgrVersion = QTVRUtils_GetQTVRVersion();        // get the version of the QuickTime VR Manager
  76. #if TARGET_OS_WIN32
  77.             // initialize the QuickTime VR Manager
  78.             InitializeQTVR();
  79. #endif
  80.         }
  81.     }    // end of kInitAppPhase_BeforeCreateFrameWindow
  82.  
  83.     // ***do any start-up activities that should occur after the MDI frame window is created
  84.     if (theStartPhase & kInitAppPhase_AfterCreateFrameWindow) {
  85. #if TARGET_OS_WIN32
  86.         // on Windows, open as movie documents any files specified on the command line
  87.         SendMessage(ghWnd, WM_OPENDROPPEDFILES, 0L, 0L);
  88. #endif
  89.     }    // end of kInitAppPhase_AfterCreateFrameWindow
  90. }
  91.  
  92.  
  93. //////////
  94. //
  95. // StopApplication
  96. // Do any application-specific shut-down.
  97. //
  98. // The theStopPhase parameter determines which "phase" of application shut-down is executed,
  99. // *before* any open movie windows are destroyed or *after*.
  100. //
  101. //////////
  102.  
  103. void StopApplication (UInt32 theStopPhase)
  104. {
  105.     // @@@INSERT APPLICATION-SPECIFIC SHUT-DOWN FUNCTIONALITY HERE
  106.     
  107.     // do any shut-down activities that should occur after the movie windows are destroyed
  108.     if (theStopPhase & kStopAppPhase_AfterDestroyWindows) {
  109.         
  110. #if TARGET_OS_WIN32
  111.         // terminate QuickTime VR Manager
  112.         if (gQTVRMgrIsPresent)
  113.             TerminateQTVR();
  114. #endif
  115.             
  116.     }    // end of kStopAppPhase_AfterDestroyWindows
  117. }
  118.  
  119.  
  120. //////////
  121. //
  122. // DoIdle
  123. // Do any processing that can/should occur at idle time.
  124. //
  125. //////////
  126.  
  127. void DoIdle (WindowReference theWindow)
  128. {
  129.     WindowObject         myWindowObject = NULL;
  130.     GrafPtr             mySavedPort;
  131.     
  132.     GetPort(&mySavedPort);
  133.     MacSetPort(GetPortFromWindowReference(theWindow));
  134.     
  135.     myWindowObject = GetWindowObjectFromWindow(theWindow);
  136.     if (myWindowObject != NULL) {
  137.         MovieController        myMC = NULL;
  138.     
  139.         myMC = (**myWindowObject).fController;
  140.         if (myMC != NULL) {
  141.  
  142. #if TARGET_OS_MAC
  143.             // restore the cursor to the arrow
  144.             // if it's outside the front movie window or outside the window's visible region
  145.             if (theWindow == GetFrontMovieWindow()) {
  146.                 Rect    myRect;
  147.                 Point    myPoint;
  148.                 
  149.                 GetMouse(&myPoint);
  150.                 MCGetControllerBoundsRect(myMC, &myRect);
  151.                 if (!MacPtInRect(myPoint, &myRect) || !PtInRgn(myPoint, GetPortFromWindowReference(theWindow)->visRgn))
  152.                     MacSetCursor(&qd.arrow);
  153.             }
  154. #endif    // TARGET_OS_MAC
  155.         }
  156.     }
  157.     
  158.     // @@@INSERT APPLICATION-SPECIFIC IDLE-TIME FUNCTIONALITY HERE
  159.     
  160.     MacSetPort(mySavedPort);
  161. }
  162.  
  163.  
  164. //////////
  165. //
  166. // DoUpdateWindow
  167. // Update the specified window.
  168. //
  169. //////////
  170.  
  171. void DoUpdateWindow (WindowReference theWindow, Rect *theRefreshArea)
  172. {
  173.     GrafPtr             mySavedPort;
  174.     
  175.     GetPort(&mySavedPort);
  176.     MacSetPort(GetPortFromWindowReference(theWindow));
  177.     
  178.     BeginUpdate(GetPortFromWindowReference(theWindow));
  179.     //EraseRect(theRefreshArea);        // this is important, for non-rectangular movies
  180.     
  181.     // @@@INSERT APPLICATION-SPECIFIC DRAWING FUNCTIONALITY HERE
  182.     
  183.     // draw the movie controller and its movie
  184.     MCDoAction(GetMCFromWindow(theWindow), mcActionDraw, theWindow);
  185.     
  186.     EndUpdate(GetPortFromWindowReference(theWindow));
  187.     MacSetPort(mySavedPort);
  188. }
  189.  
  190.  
  191. //////////
  192. //
  193. // HandleContentClick
  194. // Handle mouse button clicks in the specified window.
  195. //
  196. //////////
  197.  
  198. void HandleContentClick (WindowReference theWindow, EventRecord *theEvent)
  199. {
  200. #pragma unused(theEvent)
  201.  
  202.     GrafPtr             mySavedPort;
  203.     
  204.     GetPort(&mySavedPort);
  205.     MacSetPort(GetPortFromWindowReference(theWindow));
  206.     
  207.     // @@@INSERT APPLICATION-SPECIFIC CONTENT CLICKING FUNCTIONALITY HERE
  208.  
  209.     MacSetPort(mySavedPort);
  210. }
  211.  
  212.  
  213. //////////
  214. //
  215. // HandleApplicationKeyPress
  216. // Handle application-specific key presses.
  217. // Returns true if the key press was handled, false otherwise.
  218. //
  219. //////////
  220.  
  221. Boolean HandleApplicationKeyPress (char theCharCode)
  222. {
  223.     Boolean        isHandled = true;
  224.     
  225.     switch (theCharCode) {
  226.     
  227.         // @@@HANDLE APPLICATION-SPECIFIC KEY PRESSES HERE
  228.  
  229.         default:
  230.             isHandled = false;
  231.             break;
  232.     }
  233.  
  234.     return(isHandled);
  235. }
  236.  
  237.  
  238. #if TARGET_OS_MAC
  239. //////////
  240. //
  241. // CreateMovieWindow
  242. // Create a window to display a movie in.
  243. //
  244. //////////
  245.  
  246. WindowRef CreateMovieWindow (Rect *theRect, Str255 theTitle)
  247. {
  248.     WindowRef            myWindow;
  249.     
  250.     myWindow = NewCWindow(NULL, theRect, theTitle, false, noGrowDocProc, (WindowPtr)-1L, true, 0);
  251.     return(myWindow);
  252. }
  253. #endif
  254.  
  255.  
  256. //////////
  257. //
  258. // HandleApplicationMenu
  259. // Handle selections in the application's menus.
  260. //
  261. // The theMenuItem parameter is a UInt16 version of the Windows "menu item identifier". 
  262. // When called from Windows, theMenuItem is simply the menu item identifier passed to the window proc.
  263. // When called from MacOS, theMenuItem is constructed like this:
  264. //     *high-order 8 bits == the Macintosh menu ID (1 thru 256)
  265. //     *low-order 8 bits == the Macintosh menu item (sequential from 1 to ordinal of last menu item in menu)
  266. // In this way, we can simplify the menu-handling code. There are, however, some limitations,
  267. // mainly that the menu item identifiers on Windows must be derived from the Mac values. 
  268. //
  269. //////////
  270.  
  271. void HandleApplicationMenu (UInt16 theMenuItem)
  272. {    
  273.     switch (theMenuItem) {
  274.         case IDM_MAKE_OBJECT_MOVIE:
  275.             VRObject_PromptUserForMovieFileAndMakeObject();
  276.             break;
  277.         
  278.         case IDM_CREATE_VERSION_1x0:
  279.             gVersionToCreate = kQTVRVersion1;
  280.             break;
  281.         
  282.         case IDM_CREATE_VERSION_2x0:
  283.             gVersionToCreate = kQTVRVersion2;
  284.             break;
  285.         
  286.         default:
  287.             break;
  288.     }    // switch (theMenuItem)
  289. }
  290.  
  291.  
  292. //////////
  293. //
  294. // AdjustApplicationMenus
  295. // Adjust state of items in the application's menus.
  296. //
  297. // Currently, the Mac application has only one app-specific menu ("Test"); you could change that.
  298. //
  299. //////////
  300.  
  301. void AdjustApplicationMenus (WindowReference theWindow, MenuReference theMenu)
  302. {
  303.     MenuReference        myMenu;
  304.     
  305. #if TARGET_OS_WIN32
  306.     myMenu = theMenu;
  307. #elif TARGET_OS_MAC
  308.     myMenu = GetMenuHandle(kTestMenu);
  309. #endif
  310.     
  311.     // we don't allow creating new files here...
  312. #if TARGET_OS_MAC
  313.     SetMenuItemState(GetMenuHandle(mFile), iNew, kDisableMenuItem);
  314. #endif
  315.  
  316.     SetMenuItemCheck(myMenu, IDM_CREATE_VERSION_1x0, (gVersionToCreate == kQTVRVersion1));
  317.     SetMenuItemCheck(myMenu, IDM_CREATE_VERSION_2x0, (gVersionToCreate == kQTVRVersion2));
  318. }
  319.  
  320.  
  321. //////////
  322. //
  323. // DoApplicationEventLoopAction
  324. // Perform any application-specific event loop actions.
  325. //
  326. // Return true to indicate that we've completely handled the event here, false otherwise.
  327. //
  328. //////////
  329.  
  330. Boolean DoApplicationEventLoopAction (EventRecord *theEvent)
  331. {
  332. #pragma unused(theEvent)
  333.  
  334.     return(false);            // no-op for now
  335. }
  336.  
  337.  
  338. //////////
  339. //
  340. // AddControllerFunctionality
  341. // Configure the movie controller.
  342. //
  343. //////////
  344.  
  345. void AddControllerFunctionality (MovieController theMC)
  346. {
  347.     long            myControllerFlags;
  348.     
  349.     // CLUT table use
  350.     MCDoAction(theMC, mcActionGetFlags, &myControllerFlags);
  351.     MCDoAction(theMC, mcActionSetFlags, (void *)(myControllerFlags | mcFlagsUseWindowPalette));
  352.  
  353.     // enable keyboard event handling
  354.     MCDoAction(theMC, mcActionSetKeysEnabled, (void *)true);
  355.     
  356.     // disable drag support
  357.     MCDoAction(theMC, mcActionSetDragEnabled, (void *)false);
  358. }
  359.  
  360.  
  361. //////////
  362. //
  363. // InitApplicationWindowObject
  364. // Do any application-specific initialization of the window object.
  365. //
  366. //////////
  367.  
  368. void InitApplicationWindowObject (WindowObject theWindowObject)
  369. {
  370.     Track                    myQTVRTrack = NULL;
  371.     Movie                    myMovie = NULL;
  372.     MovieController            myMC = NULL;
  373.     QTVRInstance            myInstance = NULL;
  374.         
  375.     if (theWindowObject == NULL)
  376.         return;
  377.  
  378.     // make sure we can safely call the QTVR API
  379.     if (!gQTVRMgrIsPresent)
  380.         return;
  381.  
  382.     // find the QTVR track, if there is one
  383.     myMC = (**theWindowObject).fController;
  384.     myMovie = (**theWindowObject).fMovie;
  385.     myQTVRTrack = QTVRGetQTVRTrack(myMovie, 1);
  386.     
  387.     QTVRGetQTVRInstance(&myInstance, myQTVRTrack, myMC);
  388.     (**theWindowObject).fInstance = myInstance;
  389.  
  390.     // do any QTVR window configuration
  391.     if (myInstance != NULL) {
  392.  
  393.         // set unit to radians
  394.         QTVRSetAngularUnits(myInstance, kQTVRRadians);
  395.  
  396.         // update
  397.         QTVRUpdate(myInstance, kQTVRCurrentMode);
  398.     }
  399. }
  400.  
  401.  
  402. //////////
  403. //
  404. // RemoveApplicationWindowObject
  405. // Do any application-specific clean-up of the window object.
  406. //
  407. //////////
  408.  
  409. void RemoveApplicationWindowObject (WindowObject theWindowObject)
  410. {
  411.     // @@@INSERT APPLICATION-SPECIFIC WINDOW OBJECT CLEAN-UP HERE
  412.  
  413.     // DoDestroyMovieWindow in MacFramework.c or MovieWndProc in WinFramework.c
  414.     // releases the window object itself
  415. }
  416.  
  417.  
  418. //////////
  419. //
  420. // ApplicationMCActionFilterProc 
  421. // Intercept some mc actions for the movie controller.
  422. //
  423. // NOTE: The theRefCon parameter is a handle to a window object record.
  424. //
  425. //////////
  426.  
  427. PASCAL_RTN Boolean ApplicationMCActionFilterProc (MovieController theMC, short theAction, void *theParams, long theRefCon)
  428. {
  429. #pragma unused(theMC)
  430.  
  431.     Boolean                isHandled = false;            // false => allow controller to process the action
  432.     WindowObject        myWindowObject = NULL;
  433.     
  434.     myWindowObject = (WindowObject)theRefCon;
  435.     if (myWindowObject == NULL)
  436.         return(isHandled);
  437.         
  438.     switch (theAction) {
  439.     
  440.         // handle window resizing
  441.         case mcActionControllerSizeChanged:
  442.             SizeWindowToMovie(myWindowObject);
  443.             break;
  444.  
  445.         // handle idle events
  446.         case mcActionIdle:
  447.             DoIdle((**myWindowObject).fWindow);
  448.             break;
  449.             
  450.         default:
  451.             break;
  452.             
  453.     }    // switch (theAction)
  454.     
  455.     return(isHandled);    
  456. }
  457.  
  458.  
  459.